package com.lemon.android.http;

import android.content.Context;

import com.lemon.smartsafety.BuildConfig;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.utils.app.SharedPreferencesHelper;
import com.lemon.smartsafety.utils.common.AppUtils;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Cooliean on 14-6-17.
 */
public class LemonRequestParams extends HashMap {
    private List<FormField> list;

    public static final String _txtIdValues = "_txtIdValues";
    public static final String MOBILE_TYPE  = "Android";

    public LemonRequestParams(String tableName, String idField) {
        super();
        list = new ArrayList<FormField>();
        requestParams(tableName, idField, "");
    }

    public LemonRequestParams(String tableName, String idField, String idPrefix) {
        super();
        list = new ArrayList<FormField>();
        requestParams(tableName, idField, idPrefix);
    }

    // add by Andrew.yin 2017/8/18 begin

    /**
     *
     * @param token 传空字串""就可以
     */
    public LemonRequestParams(String token) {
        super();
        list = new ArrayList<FormField>();
        requestParams(token);
    }

    private void requestParams(String token) {
        User user = AccountUtil.getUser();
        if(user != null) {
            this.put("token", user.getToken());
        } else {
            this.put("token", "");
        }
        this.put("mobile_type", MOBILE_TYPE);
        this.put("is_mobile", "1");
    }
    // add by Andrew.yin 2017/8/18 end

    private void requestParams(String tableName, String idField, String idPrefix) {
        this.put("_txtDataSourceProvider", "");
        this.put("_txtDebug", "1");
        this.put("_txtIdFields", idField);
        this.put("_txtIdNumeric", "1");
        this.put("_txtIdPrefix", idPrefix);
        this.put("_txtIndexFields", "");
        this.put("_txtIndexFilters", "");
        this.put("_txtRadioFields", "");
        this.put("_txtRadioFilters", "");
        this.put("_txtRemarkFields", "");
        this.put("_txtTableName", tableName);
        this.put("is_mobile", "1");
        if (BuildConfig.DEBUG) {
            this.put("is_mobile_debug", "1");
        }
        //changed by Andrew.yin 2017/7/27 begin
        //this.put("user_id", new SharedPreferencesHelper(IApplication.context()).getUserId()); //disable by Andrew.yin 2017/7/27
        User user = AccountUtil.getUser();
        if(user != null) {
            this.put("user_id", user.getUserId());
        } else {
            this.put("user_id", "");
        }
        //changed by Andrew.yin 2017/7/27 end
        this.put("rp", Constants.RP_PASSWORD);
        this.put("mobile_type", MOBILE_TYPE);
        this.put("version_code", AppUtils.getVersionCode(IApplication.context()) + "");
    }


    public LemonRequestParams() {
        super();
        list = new ArrayList<FormField>();
        this.put("is_mobile", "1");
        if (BuildConfig.DEBUG) {
            this.put("is_mobile_debug", "1");
        }
        this.put("rp", Constants.RP_PASSWORD);
        this.put("mobile_type", MOBILE_TYPE);

        //changed by Andrew.yin 2017/7/27 begin
        //this.put("user_id", new SharedPreferencesHelper(IApplication.context()).getUserId());
        User user = AccountUtil.getUser();
        if(user != null) {
            this.put("user_id", user.getUserId());
        } else {
            this.put("user_id", "");
        }
        //changed by Andrew.yin 2017/7/27 end
        this.put("version_code", AppUtils.getVersionCode(IApplication.context()) + "");

    }

    public static LemonRequestParams getInstance() {
        return new LemonRequestParams();
    }

    public LemonRequestParams putParam(String key, String value) {
        this.put(key, value);
        return this;
    }


    public LemonRequestParams putParam(String key, String value, int fieldType) {
        this.put(key, value, fieldType);
        return this;
    }

    public LemonRequestParams putAll(Map<String, String> map, int fieldType) {
        Set<String> myKeys = map.keySet();
        for (String key : myKeys) {
            this.put(key, map.get(key), fieldType);
        }
        return this;
    }

    /**
     * 直接使用 LemonRequestParams();
     *
     * @param context
     */
    @Deprecated
    public LemonRequestParams(Context context) {
        super();
        list = new ArrayList<FormField>();
        this.put("is_mobile", "1");
        if (BuildConfig.DEBUG) {
            this.put("is_mobile_debug", "1");
        }
        this.put("rp", Constants.RP_PASSWORD);
        this.put("mobile_type", MOBILE_TYPE);

        //changed by Andrew.yin 2017/7/27 begin
        //this.put("user_id", StringUtils.nullValue(new SharedPreferencesHelper(IApplication.context()).getUserId(), ""));
        User user = AccountUtil.getUser();
        if(user != null) {
            this.put("user_id", user.getUserId());
        } else {
            this.put("user_id", "");
        }
        //changed by Andrew.yin 2017/7/27 end
        this.put("version_code", AppUtils.getVersionCode(IApplication.context()) + "");
    }


    public LemonRequestParams putUserId(Context context) {

        //changed by Andrew.yin 2017/7/27 begin
        //this.put("user_id", new SharedPreferencesHelper(context).getUserId());
        User user = AccountUtil.getUser();
        if(user != null) {
            this.put("user_id", user.getUserId());
        } else {
            this.put("user_id", "");
        }
        //changed by Andrew.yin 2017/7/27 end
        return this;
    }


    @Override
    public Object put(Object key, Object value) {
        if (null == value) {
            value = "";
        }
        if (value instanceof Integer) {
            value = "" + value;
        }
        return super.put(key, value);
    }

    public void put(String key, String value, int fieldType) {
        put(key, value, fieldType, false);
    }

    public void put(String key, String value, int fieldType, boolean isOtherTableField) {
        if (key != null && value != null) {
            this.put(key, value);
        }
        FormField formField = new FormField(key, fieldType, isOtherTableField ? 1 : 0);
        list.add(formField);
        this.put("_txtFormObjects", list.toString().trim().replaceAll("\\[{1}|\\s.*?|\\]{1}", ""));
    }

    /**
     * 过滤掉某些Script 标签
     *
     * @param oldStr
     * @return
     */
    public String filterScript(String oldStr) {
        return oldStr.replaceAll("<script .*>(\\\\s|.)*</script>", "");
    }
}
